package com.qing.common.view;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.util.SparseIntArray;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.qing.common.R;
import com.qing.common.base.BaseButton;

/**
 * @author qing <br/>
 * 2025/3/2
 */
public class DeviceButton extends BaseButton {

    public static final int TYPE_HEADSET = 0x01;
    public static final int TYPE_PHONE = 0x02;
    public static final int TYPE_HEADSET_READING = 0x03;
    public static final int TYPE_PHONE_READING = 0x04;
    private static final int INFO_FRAME_MIN = INFO_BACKGROUND + 1;
    private static final int INFO_FRAME_MAX = INFO_BACKGROUND + 2;
    private static final int INFO_FRAME_START = INFO_BACKGROUND + 3;
    private int mLastRes = -1;

    public DeviceButton(@NonNull Context context) {
        super(context);
    }

    public DeviceButton(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public DeviceButton(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public DeviceButton(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onInit() {
        mTypeInfos.put(TYPE_HEADSET, new SparseIntArray() {
            {
                put(INFO_CONTENT, R.raw.r_headset);
                put(INFO_BACKGROUND, R.drawable.shape_r16_ff38b6e3_0a38b6e3);
                put(INFO_FRAME_MIN, 0);
                put(INFO_FRAME_MAX, 5);
                put(INFO_FRAME_START, 0);
            }
        });
        mTypeInfos.put(TYPE_PHONE, new SparseIntArray() {
            {
                put(INFO_CONTENT, R.raw.r_phone);
                put(INFO_BACKGROUND, R.drawable.shape_r16_ffffffff);
                put(INFO_FRAME_MIN, 0);
                put(INFO_FRAME_MAX, 5);
                put(INFO_FRAME_START, 0);

            }
        });
        mTypeInfos.put(TYPE_HEADSET_READING, new SparseIntArray() {
            {
                put(INFO_CONTENT, R.raw.r_headset);
                put(INFO_BACKGROUND, R.drawable.shape_r16_ff38b6e3_0a38b6e3);
                put(INFO_FRAME_MIN, 5);
                put(INFO_FRAME_MAX, 75);
                put(INFO_FRAME_START, 5);
            }
        });
        mTypeInfos.put(TYPE_PHONE_READING, new SparseIntArray() {
            {
                put(INFO_CONTENT, R.raw.r_phone);
                put(INFO_BACKGROUND, R.drawable.shape_r16_ffffffff);
                put(INFO_FRAME_MIN, 5);
                put(INFO_FRAME_MAX, 75);
                put(INFO_FRAME_START, 5);
            }
        });
        setType(TYPE_HEADSET);
    }

    @Override
    protected void onTypeChanged(int oldType, int newType) {
        SparseIntArray info = mTypeInfos.get(newType);
        SparseIntArray oldInfo = mTypeInfos.get(oldType);
        int rawRes = info.get(INFO_CONTENT);
        if (rawRes != mLastRes) {
            mLastRes = rawRes;
            getImageView().setAnimation(rawRes);
            if (oldType == 0) {
                getImageView().setFrame(info.get(INFO_FRAME_START));
                return;
            }
        }
        getImageView().setRepeatCount(0);
        if (newType == TYPE_PHONE_READING || newType == TYPE_HEADSET_READING) {
            getImageView().setMinAndMaxFrame(oldInfo.get(INFO_FRAME_MIN), oldInfo.get(INFO_FRAME_MAX));
        } else {
            getImageView().setMinAndMaxFrame(info.get(INFO_FRAME_MIN), info.get(INFO_FRAME_MAX));
        }
        if ((newType == TYPE_PHONE && oldType == TYPE_PHONE_READING) || (newType == TYPE_HEADSET && oldType == TYPE_HEADSET_READING)) {
            // 停止，反向播放
            getImageView().setFrame(info.get(INFO_FRAME_MAX));
        } else {
            getImageView().setFrame(info.get(INFO_FRAME_START));
        }
        getImageView().setSpeed(newType == TYPE_PHONE_READING || newType == TYPE_HEADSET_READING ? 1f : -1f);
        getImageView().playAnimation();
        getImageView().addAnimatorListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                getImageView().removeAnimatorListener(this);
                if (newType == TYPE_PHONE || newType == TYPE_HEADSET) {
                    getImageView().setFrame(info.get(INFO_FRAME_MIN));
                } else if (newType == TYPE_PHONE_READING || newType == TYPE_HEADSET_READING) {
                    getImageView().setFrame(info.get(INFO_FRAME_START));
                    getImageView().setRepeatCount(ValueAnimator.INFINITE);
                    getImageView().setMinAndMaxFrame(info.get(INFO_FRAME_MIN), info.get(INFO_FRAME_MAX));
                    getImageView().playAnimation();
                }
            }
        });
        setBackgroundResource(info.get(INFO_BACKGROUND));
    }
}
